package com.spzx.order.receiver;

import com.spzx.common.rabbit.constant.MqConst;
import com.rabbitmq.client.Channel;
import com.spzx.order.configure.DeadLetterMqConfig;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Slf4j
@Component
public class TestReceiver {
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(exchange = @Exchange(value = MqConst.EXCHANGE_TEST,declare = "ture"),
    value = @Queue(value = MqConst.QUEUE_TEST,declare = "ture"),
    key = {MqConst.ROUTING_TEST}))
    public void a(Message message, Channel channel, String hello) throws IOException {
        byte[] body = message.getBody();
        String msg = new String(body);
        System.out.println(MqConst.QUEUE_TEST+"队列监听："+msg+" "+hello);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);// 手动应答

    }
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_TEST, durable = "true"),
            value = @Queue(value = MqConst.QUEUE_CONFIRM, durable = "true"),
            key = MqConst.ROUTING_CONFIRM
    ))
    public void confirm(String content, Message message, Channel channel) {
        log.info("接收确认消息：{}", content);

        // false 确认一个消息，true 批量确认
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
    @SneakyThrows
    @RabbitListener(queues = {DeadLetterMqConfig.queue_dead_2})
    public void getDeadLetterMsg(String msg, Message message, Channel channel){
        log.info("死信消费者：{}", msg);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
